package com.example.libraryapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.libraryapp.R;
import com.example.libraryapp.activity.borrowHistory.BorrowHistoryActivity;
import com.example.libraryapp.activity.favorite.FavoriteActivity;
import com.example.libraryapp.activity.login.LoginActivity;
import com.example.libraryapp.activity.personal.PersonalActivity;
import com.example.libraryapp.bean.User;
import com.example.libraryapp.dialog.ExitDialog;
import com.example.libraryapp.dialog.OnExitDialogListener;
import com.example.libraryapp.network.base.BaseFragment;
import com.example.libraryapp.network.base.BasePresenter;
import com.example.libraryapp.util.LoginUtil;
import com.example.libraryapp.util.ToastUtil;
import com.example.libraryapp.widget.CnToolbar;

/**
 * @author 伍凯铭
 * @since 2020/6/18
 */
public class MineFragment extends BaseFragment implements View.OnClickListener, OnExitDialogListener {

    private TextView mNameText;

    private TextView mExitBtn;

    private ExitDialog mExitDialog;

    private User nowUser;

    @Override
    public void onStart() {
        super.onStart();
        checkLogin();
    }

    private void checkLogin() {
        nowUser = LoginUtil.getNowUser(getContext());
        if(nowUser != null) {
            mNameText.setText(nowUser.getRealName());
            mNameText.setTextColor(ContextCompat.getColor(getContext(),R.color.black));
            mExitBtn.setVisibility(View.VISIBLE);
        }else {
            mNameText.setText(R.string.log_and_register);
            mNameText.setTextColor(ContextCompat.getColor(getContext(),R.color.blue));
            mExitBtn.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initView(View view) {
        mNameText = view.findViewById(R.id.name_txt);
        mExitBtn = view.findViewById(R.id.tv_exit);
        view.findViewById(R.id.mine_card).setOnClickListener(this);
        view.findViewById(R.id.set_password).setOnClickListener(this);
        view.findViewById(R.id.collect).setOnClickListener(this);
        view.findViewById(R.id.borrow).setOnClickListener(this);
        view.findViewById(R.id.about).setOnClickListener(this);
        mExitBtn.setOnClickListener(this);
        mExitDialog = new ExitDialog(getActivity(),this);
    }

    @Override
    public void sure() {
        LoginUtil.logout(getContext());
        checkLogin();
    }

    @Override
    public void cancel() {

    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.about) {
            ToastUtil.showToast(getContext(),"鸣谢孙笑川哥哥为我们带来的带制作");
            return;
        }
        if(nowUser == null) {
            if(v.getId() != R.id.mine_card) {
                ToastUtil.showToast(getContext(),"请先登录");
            }
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        }else {
            switch (v.getId()) {
                case R.id.mine_card:
                    Intent intent1 = new Intent(getActivity(), PersonalActivity.class);
                    startActivity(intent1);
                    break;
                case R.id.set_password:
                    ToastUtil.showToast(getContext(),"敬请期待");
                    break;
                case R.id.borrow:
                    Intent intent = new Intent(getActivity(), BorrowHistoryActivity.class);
                    startActivity(intent);
                    break;
                case R.id.collect:
                    Intent intent2 = new Intent(getActivity(), FavoriteActivity.class);
                    startActivity(intent2);
                    break;
                case R.id.tv_exit:
                    mExitDialog.show();
                    break;
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected void initToolBar(CnToolbar toolbar) {
        toolbar.setTitle("我的");
    }
}
